﻿//------------------------------------------------------------------------------
// <copyright file="ConfigurationTest.cs" company="CommerceServer.net, Inc">
//    (c) 2012 CommerceServer.net, Inc. and its affiliates. All rights reserved.
// </copyright>
// <summary></summary>
//------------------------------------------------------------------------------ 
using System;
using System.Configuration;
using CommerceServerContrib.CodeGeneration.ConfigurationHelpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CommerceServer.Foundation;

namespace CommerceServerContrib.CodeGeneration.ConfigurationTest
{
    /// <summary>
    /// Summary description for ConfigurationTest
    /// </summary>
    [TestClass]
    public class ConfigurationTest
    {
        public ConfigurationTest()
        {
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void InvokeAPI()
        {
            string configurationFilename = @"sample.config";

            ExeConfigurationFileMap configurationFile = new ExeConfigurationFileMap();
            configurationFile.ExeConfigFilename = configurationFilename;
            var config = ConfigurationManager.OpenMappedExeConfiguration(configurationFile, ConfigurationUserLevel.None);
            using (var configSwapper = new FullConfigurationSwapper(config.FilePath))
            {
                var simpleCatalogQuery = new CommerceQuery<CommerceEntity>("Catalog");
                simpleCatalogQuery.SearchCriteria.Model.Id = "Adventure Works Catalog";
                var operationServiceAgent = new OperationServiceAgent();
                
                CommerceRequestContext requestContext = new CommerceRequestContext()
                {
                    Channel = "",
                    UserLocale = "en-US",
                    UserUILocale = "en-US",
                    RequestId = Guid.NewGuid().ToString()
                };

                var response = operationServiceAgent.ProcessRequest(requestContext, simpleCatalogQuery.ToRequest());
                Assert.IsNotNull(response, "Response null");
                var commerceQueryOperationResponse = response.OperationResponses[0] as CommerceQueryOperationResponse;
                Assert.IsNotNull(commerceQueryOperationResponse, "Commerce Query Operation Response is null");
            }
        }

        [TestMethod]
        public void RequestTemplateGroupTest()
        {
            string channel = "";
            string userLocal = "en-US";
            string userUILocale = "en-US";


            var requestTemplateGenerator = new RequestTemplateGenerator("sample.config", channel, userLocal, userUILocale);
            var groups = requestTemplateGenerator.GetRequestTemplates();
            Assert.IsNotNull(groups, "Groups is null");
        }
    }
}
